//
//  PackageScrollView.m
//  测试
//
//  Created by 陶一平 on 16/7/31.
//  Copyright © 2016年 陶一平. All rights reserved.
//
#define S_WIDTH [[UIScreen mainScreen] bounds].size.width
#define S_HEIGHT 110
#import "PackageScrollView.h"

@interface PackageScrollView ()<UIScrollViewDelegate>
{
    NSInteger _currentIndex;
    NSTimer *_timer;
}
@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UIPageControl *pageControl;
@property (nonatomic,strong)NSArray *images;

@end
@implementation PackageScrollView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self= [super initWithFrame:frame]) {
        [self addSubview:self.scrollView];
        [self addSubview:self.pageControl];
        [self scrollViewAddSubView];
        [self starTimer];
    }
    return self;
}
-(UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 110)];
        _scrollView.backgroundColor = [UIColor grayColor];
        _scrollView.contentSize = CGSizeMake(S_WIDTH*self.images.count, 0);
        _scrollView.bounces = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.scrollsToTop = YES;
        _scrollView.contentOffset = CGPointMake(S_WIDTH, 0);
        _scrollView.delegate = self;
    }
    return _scrollView;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x >= (_currentIndex+1)*S_WIDTH) {
        _currentIndex++;
        if (_currentIndex == self.images.count-1) {
            scrollView.contentOffset = CGPointMake(S_WIDTH, 0);
            _currentIndex = 1;
        }
        _pageControl.currentPage = _currentIndex-1;
    }
    if (scrollView.contentOffset.x<=(_currentIndex -1)*S_WIDTH) {
        _currentIndex--;
        if (_currentIndex == 0) {
            scrollView.contentOffset = CGPointMake((self.images.count - 2)*S_WIDTH, 0);
        }
        _pageControl.currentPage = _currentIndex-1;
    }
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self stopTimer];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
}
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self starTimer];
}
- (void)starTimer{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(scroll) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
    }
}
- (void)stopTimer{
    if (_timer.valid) {
        [_timer invalidate];
        _timer = nil;
    }
}
- (void)scroll{
    [_scrollView setContentOffset:CGPointMake(_scrollView.contentOffset.x+S_WIDTH, 0) animated:YES];
}
- (void)scrollViewAddSubView{
    for (int i = 0; i <self.images.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(S_WIDTH*i, 0, S_WIDTH, S_HEIGHT)];
        imageView.image = [UIImage imageNamed:_images[i]];
        [_scrollView addSubview:imageView];
    }
}
-(UIPageControl *)pageControl{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, 375/2, 30)];
        _pageControl.center  = CGPointMake(_scrollView.center.x, CGRectGetMaxY(_scrollView.frame) -30);
        _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];

        _pageControl.currentPageIndicatorTintColor = [UIColor blueColor];

        _pageControl.numberOfPages = self.images.count-2;

        _pageControl.currentPage = 0;
        
    }
    return _pageControl;
}
-(NSArray *)images{
    if (!_images) {
        _images = [NSArray arrayWithObjects:@"首页-picture",@"首页-picture",@"首页-picture",@"首页-picture", nil];
    }
    return _images;
}
@end
